/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mediana;

import Mutli.SortThread;
import java.util.ArrayList;
import java.util.concurrent.Semaphore;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mateusz.ledzewicz
 */
public class Mediana {
    private final static int N = 279;
    public final static int RandMax = 100;
    public final static int BeginThreadNumber = 4;
    private final static int LimitThreadNumber = 4;
    

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            // TODO code application logic here
            Long beginTime, endTime;
            Table myTable= new Table(N);
            Table myTable2 = new Table(myTable);
            beginTime = System.nanoTime();
                multithread(myTable);
            endTime = System.nanoTime();
            System.out.println((endTime-beginTime)/1000000 + " ms");
            if(!myTable.isSorted()) throw new Exception("Not sorted table");
          
            beginTime = System.nanoTime();
                normal(myTable2);
            endTime = System.nanoTime();
            System.out.println((endTime-beginTime)/1000000 + " ms");
            if(!myTable2.isSorted()) throw new Exception("Not sorted table");
            
            
            
        } catch (Exception ex) {
            Logger.getLogger(Mediana.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        
        
     
    }
    
    public static void normal(Table table){
        quickSort(table, new Range(0, table.getSize()-1));
        System.out.println("Mediana2 = " + table.getMedian());
        
    }
    
    public static void multithread(Table myTable){
          Semaphore sem = new Semaphore(1);
          Bufor.init(myTable);
            ArrayList<SortThread> l = new ArrayList();
            for(int i = 0; i<BeginThreadNumber;i++){
                
                SortThread s = new SortThread(sem, myTable);
                s.start();
                l.add(s);
                
            }
            
            while(Thread.activeCount() > 1){}
         
           System.out.println("Mediana = " + myTable.getMedian());
    }
    
    public static void quickSort(Table table, Range range){
        int pivot = (range.getMax() + range.getMin())/2;
        int pivotValue = table.getElement(pivot);
        table.swap(pivot, range.getMax());
        int j = range.getMin();
            
        for(int i=range.getMin(); i<=range.getMax()-1;i++){
            if(table.getElement(i) >= pivotValue) continue;
            table.swap(i, j);
            j++;
        }
        table.swap(range.getMax(), j);
           
        if(j+1 < range.getMax())
            quickSort(table,new Range(j+1, range.getMax()));
        if(j-1 > range.getMin())
            quickSort(table, new Range(range.getMin(), j-1));
         
    }
    
}
